# -*- encoding=utf8 -*-
__author__ = "chen"

import threading
import tkinter as tk  # 创建窗口
import win32gui
from airtest.core.api import *
from multiprocessing import Process
from common.common_util import *
from 抓鬼.捉鬼任务 import *
from 藏宝图.挖图任务 import run_mask_watu

root = tk.Tk()
root.title("任务执行") #设置窗口标题
root.geometry("600x800") #设置窗口尺寸

def get_all_windows():
    hwnd_list = []
    # 获取所有子窗口
    hwnds_title = []
    win32gui.EnumWindows(lambda hwnd, param: param.append(hwnd), hwnd_list)
    for hwnd in hwnd_list:
        title = win32gui.GetWindowText(hwnd)
        if (title.find("记事本")>= 0):
            print("句柄：", hwnd, "标题：", title)
            hwnds_title.append([hwnd,title])
    return hwnds_title

#############
def check_selection():
    selected_items = []
    for i, var in enumerate(check_vars):
        if var.get() == 1:  # 如果Checkbutton被选中
            selected_items.append(options[i])
    print("Selected items:", selected_items)
    result_label.config(text="选择对象：" + str(selected_items))
    return selected_items

def check_selection_zhuogui():
    # 捉鬼任务
    selected_items = check_selection()
    # 在这里添加你的下一步动作
    threading.Thread(target=run_mask_zhuogui, args=(selected_items,10)).start()

def check_selection_cangbaotu():
    # 捉鬼任务
    selected_items = check_selection()
    # 在这里添加你的下一步动作

    threading.Thread(target=run_mask_watu, args=(selected_items,)).start()




# 假设这是你的选项列表
options = get_all_windows()

# 创建一个变量列表来跟踪每个Checkbutton的状态
check_vars = [tk.IntVar() for _ in options]

# 遍历选项列表并创建Checkbutton
for i, option in enumerate(options):
    cb = tk.Checkbutton(root, text=option, variable=check_vars[i],justify=tk.CENTER)
    cb.pack(anchor='c')  # 将Checkbutton 中心排列

# 创建一个按钮来检查哪些Checkbutton被选中
check_btn = tk.Button(root, text="选择设备", command=check_selection_zhuogui)
check_btn.pack(pady=20)

result_label_title = tk.Label(root, text="显示结果:",width=500, height=2,bg="#e9faff",justify="left")
result_label_title.pack()
# 创建一个标签，用于显示结果
result_label = tk.Label(root, text="",width=500, height=2,justify='center',pady=20,relief="flat",wraplength=400)
result_label.pack()

if len(options)<1:
    result_label.config(text="选择对象：未找到有效设备")


root.mainloop()